package com.mgg.auth.security;

import com.mgg.auth.service.user.UserService;
import com.mgg.common.util.EmojiFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.stereotype.Component;

/**
 * @Author lijun
 * @Date: 2018-09-04 12:00:00
 */
@Component
public class MggConnectionSignUp implements ConnectionSignUp {

	@Autowired
	private UserService userService;
	
	/* (non-Javadoc)
	 * @see org.springframework.social.connect.ConnectionSignUp#execute(org.springframework.social.connect.Connection)
	 */
	@Override
	public String execute(Connection<?> connection) {
		//根据社交用户信息默认创建用户并返回用户唯一标识

		// 新用户，创建用户信息
		String headImg = connection.getImageUrl();
		String username = connection.getDisplayName();
		username = EmojiFilter.filterEmoji(username);
		username = EmojiFilter.removeFourChar(username);
		String userId;
		try {
			userId = userService.saveProviderUser(username, headImg);
		} catch (Exception e) {
			throw new RuntimeException();
		}
		return userId;
	}

}
